package com.corner.utils;

import android.location.Location;
import android.os.Bundle;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.model.LatLng;
import com.corner.app.App;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 定位相关的工具
 */
public class LocationUtil implements LocationSource, AMapLocationListener {

    private static LocationUtil instance;
    private LocationManagerProxy mAMapLocationManager;
    private OnLocationChangedListener mListener;
    private AMapLocation currLocation;
    private List<OnLocationGetListener> onLocationGetListeners;

    private LocationUtil() {
        onLocationGetListeners = new ArrayList<>();
    }

    public static LocationUtil getInstance() {
        if (instance == null) {
            instance = new LocationUtil();
        }
        return instance;
    }

    /**
     * 定位成功后回调函数
     */
    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (amapLocation != null) {
            if (amapLocation.getAMapException().getErrorCode() == 0) {
                currLocation = amapLocation;
                if (onLocationGetListeners != null) {
                    Iterator<OnLocationGetListener> iterator = onLocationGetListeners
                            .iterator();
                    while (iterator.hasNext()) {
                        OnLocationGetListener onLocationGetListener = iterator
                                .next();
                        onLocationGetListener.onLocationGet(currLocation);
                        iterator.remove();
                    }
                }
                if (mListener != null)
                    mListener.onLocationChanged(amapLocation);// 显示系统小蓝点
            }
        }
    }

    /**
     * 激活定位
     */
    @Override
    public void activate(OnLocationChangedListener listener) {
        mListener = listener;
        startLocation();
    }

    public void startLocation() {
        if (mAMapLocationManager == null) {
            mAMapLocationManager = LocationManagerProxy.getInstance(App.application);
            //此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
            //注意设置合适的定位时间的间隔，并且在合适时间调用removeUpdates()方法来取消定位请求
            //在定位结束后，在合适的生命周期调用destroy()方法
            //其中如果间隔时间为-1，则定位只定一次
            mAMapLocationManager.requestLocationData(
                    LocationProviderProxy.AMapNetwork, 15 * 1000, 10, this);
        }
    }


    /**
     * 停止定位
     */
    @Override
    public void deactivate() {
        mListener = null;
        if (mAMapLocationManager != null) {
            mAMapLocationManager.removeUpdates(this);
            mAMapLocationManager.destroy();
        }
        mAMapLocationManager = null;
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    /**
     * 获取地点信息,不使用后记得并停止定位
     *
     * @param listener
     */
    public void getLocation(OnLocationGetListener listener) {
        if (currLocation != null) {
            listener.onLocationGet(currLocation);
        } else {
            onLocationGetListeners.add(listener);
        }
    }

    public void animateMap(final AMap map) {
        if (map != null) {
            getLocation(new OnLocationGetListener() {
                @Override
                public void onLocationGet(AMapLocation location) {
                    map.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
                }
            });
        }
    }

    public interface OnLocationGetListener {
        void onLocationGet(AMapLocation location);
    }
}
